<style itemscope>
  .layui-form-label.required:before {
    /* 这种写法也可以2*/
    content: " *";
    color: red;
  }

  .addProductionPlanForm-title .title,
  .examineProductionPlanForm-title .title {
    display: inline-block;
    margin: 20px;
    font-size: 16px;
    font-weight: bold;
    color: #333333;
    line-height: 19px;
    position: relative;
    padding: 6px 0 19px 0;
  }

  .addProductionPlanForm-title .title:after,
  .examineProductionPlanForm-title .title:after {
    content: "";
    display: block;
    position: absolute;
    left: 0;
    bottom: 0;
    width: calc(100% + 6px);
    height: 3px;
    background: #0a8b0a;
  }

  .open-form .layui-input-block {
    margin-left: 115px;
  }
</style>
<script type="text/javascript" src="../../../admin/assets/js/pdfobject.min.js"></script>
<!-- 表单弹窗 -->
<form lay-filter="taskQualityRecordForm" class="layui-form open-form">
  <div class="open-model">
    <div class="open-auto">
      <input name="id" type="hidden" />
      <div class="layui-form-item" style="margin: 10px 0" id="qualityInspectionMOBox">
        <label class="layui-form-label w-auto">质检任务单：</label>
        <div class="layui-input-inline">
          <input name="taskQualityMO" id="taskQualityMO" class="layui-input" type="text" placeholder="请选择质检任务单" readonly="readonly" style="cursor: pointer" />
          <input id="taskId" name="taskId" class="layui-input" type="hidden" placeholder="质检任务id" />
        </div>
      </div>
      <div class="examineProductionPlanForm-title">
        <h3 class="title">任务配置</h3>
      </div>
      <!-- 表单 -->
      <div class="layui-form-item layui-row">
        <!--        <div class="layui-col-md3">
                    <label class="layui-form-label w-auto required">质检性质：</label>
                    <div class="layui-input-block">
                        <input id="qualityTestingNatureName" disabled name="qualityTestingNatureName" placeholder="质检性质" type="text"
                               class="layui-input"
                               autocomplete="off"/>
                    </div>
                </div>-->

        <div class="layui-col-md3">
          <label class="layui-form-label w-auto required">质检类型：</label>
          <div class="layui-input-block">
            <input id="qcQualityTypeName" disabled name="qcQualityTypeName" placeholder="质检性质" type="text" class="layui-input" autocomplete="off" />
          </div>
        </div>

        <div class="layui-col-md3">
          <label class="layui-form-label w-auto">质检性质：</label>
          <div class="layui-input-block">
            <input id="testingTypeName" disabled name="testingTypeName" placeholder="质检性质" type="text" class="layui-input" autocomplete="off" />
          </div>
        </div>
        <div class="layui-col-md3">
          <label class="layui-form-label w-auto required">质检分类：</label>
          <div class="layui-input-block">
            <input id="testingCategoryName" disabled name="testingCategoryName" placeholder="质检分类" type="text" class="layui-input" autocomplete="off" />
          </div>
        </div>
        <div class="layui-col-md3">
          <label class="layui-form-label w-auto">开工单：</label>
          <div class="layui-input-block">
            <input id="workOrderCode" disabled name="workOrderCode" placeholder="开工单" type="text" class="layui-input" autocomplete="off" />
          </div>
        </div>
      </div>
      <div class="layui-form-item layui-row">
        <div class="layui-col-md3">
          <label class="layui-form-label w-auto">所属计划：</label>
          <div class="layui-input-block">
            <input id="planCode" disabled name="planCode" placeholder="所属计划" type="text" class="layui-input" autocomplete="off" />
          </div>
        </div>
        <div class="layui-col-md3">
          <label class="layui-form-label w-auto">物料编码：</label>
          <div class="layui-input-block">
            <input id="skuCode" disabled name="skuCode" placeholder="物料编码" type="text" class="layui-input" autocomplete="off" />
          </div>
        </div>
        <div class="layui-col-md3">
          <label class="layui-form-label w-auto">物料名称：</label>
          <div class="layui-input-block">
            <input id="skuName" disabled name="skuName" placeholder="物料名称" type="text" class="layui-input" autocomplete="off" />
          </div>
        </div>
        <div class="layui-col-md3">
          <label class="layui-form-label w-auto required">送检数量：</label>
          <div class="layui-input-block">
            <input id="submissionQuantity" disabled name="submissionQuantity" placeholder="请输入数量" type="text" class="layui-input" autocomplete="off" />
          </div>
        </div>
      </div>
      <div class="layui-form-item layui-row">
        <div class="layui-col-md3">
          <label class="layui-form-label w-auto required">送检工位：</label>
          <div class="layui-input-block">
            <input id="inspectDeviceCode" disabled name="inspectDeviceCode" placeholder="送检工位" type="text" class="layui-input" autocomplete="off" />
          </div>
        </div>
        <div class="layui-col-md3">
          <label class="layui-form-label w-auto required">载具编码：</label>
          <div class="layui-input-block">
            <input id="carrierCode" disabled name="carrierCode" placeholder="请输入载具编码" type="text" class="layui-input" autocomplete="off" />
          </div>
        </div>
        <div class="layui-col-md3">
          <label class="layui-form-label w-auto">送检备注：</label>
          <div class="layui-input-block">
            <input id="remark" disabled name="remark" placeholder="请输入备注" type="text" class="layui-input" autocomplete="off" />
          </div>
        </div>
        <div class="layui-col-md3">
          <label class="layui-form-label w-auto required">状态：</label>
          <div class="layui-input-block">
            <input id="statusDesc" disabled style="border: none" name="statusDesc" placeholder="待确认" type="text" class="layui-input" autocomplete="off" />
          </div>
        </div>
      </div>
      <div class="examineProductionPlanForm-title">
        <h3 class="title">检测样品配置</h3>
      </div>

      <div class="layui-form-item layui-row">
        <div class="layui-col-md3" id="sampleCodeBox" style="display: none">
          <label class="layui-form-label w-auto required">样品编码：</label>
          <div class="layui-input-block">
            <!--  <input id="sampleCode"   name="sampleCode" placeholder="样品编码" type="text" class="layui-input" autocomplete="off" />
-->
            <select id="sampleCode" name="sampleCode" lay-filter="sampleCode"></select>
          </div>
        </div>

        <div class="layui-col-md3">
          <label class="layui-form-label w-auto">报检人：</label>
          <div class="layui-input-block">
            <input id="modifierName" disabled name="modifierName" placeholder="报检人" type="text" class="layui-input" autocomplete="off" />
          </div>
        </div>

        <div class="layui-col-md3">
          <label class="layui-form-label w-auto">状态：</label>
          <div class="layui-input-block">
            <input id="statusName" disabled name="statusName" type="text" class="layui-input" autocomplete="off" />
          </div>
        </div>
        <div class="layui-col-md3">
          <label class="layui-form-label w-auto">检测工位：</label>
          <div class="layui-input-block">
            <input id="JCInspectDeviceCode" name="JCInspectDeviceCode" placeholder="请输入检测工位" type="text" class="layui-input" autocomplete="off" />
          </div>
        </div>
      </div>
      <div class="layui-form-item layui-row">
        <div class="layui-col-md3" id="qualityTestingResultBox" style="display: none">
          <label class="layui-form-label w-auto required">检测结果：</label>
          <div class="layui-input-block">
            <select id="qualityTestingResult" lay-search="" name="qualityTestingResult" lay-filter="orderNoInfo" class="common-select-search">
              <option value="">请选择</option>
              <option value="0">合格</option>
              <option value="1">不合格</option>
              <option value="2">其他</option>
            </select>
          </div>
        </div>
        <div class="layui-col-md3 dealWayBox" style="display: none">
          <label class="layui-form-label w-auto required">处理方式：</label>
          <div class="layui-input-block">
            <select id="dealWay" name="dealWay">
              <option value="">请选择</option>
              <option value="0">报废</option>
              <option value="1">返修</option>
            </select>
          </div>
        </div>
        <div class="layui-col-md3 dealWayBox" style="display: none">
          <label class="layui-form-label w-auto required">异常原因：</label>
          <div class="layui-input-block">
            <select id="exception" name="exception">
            </select>
          </div>
        </div>
        <div class="layui-col-md3">
          <label class="layui-form-label w-auto">检测备注：</label>
          <div class="layui-input-block">
            <input id="JCRemark" name="JCRemark" placeholder="请输入检测备注" type="text" class="layui-input" autocomplete="off" />
          </div>
        </div>
        <div class="layui-col-md3">
          <label class="layui-form-label">上传图片</label>
          <div class="layui-input-block">
            <button type="button" class="layui-btn" id="uploadBtn">
              <i class="layui-icon">&#xe67c;</i>上传图片
            </button>
            <div class="layui-upload-list">
              <img class="layui-upload-img" id="uploadPreview" style="max-width: 200px;">
              <p id="uploadText"></p>
            </div>
            <!-- 隐藏域，用于表单提交时记录图片地址 -->
            <input type="hidden" name="paperSlipPic" id="uploadInput">
          </div>
        </div>
        <!-- <div class="layui-col-md3">
          <label class="layui-form-label w-auto">上传图片：</label>
          <div class="layui-input-block" style="display: flex">
            <button class="layui-btn" type="button" id="uploadBtn">
              上传图片
            </button>
            <div
            id="img"
              style="
                margin-left: 5px;
                width: 100px;
                height: 100px;
              "
            ></div>
          </div>
        </div>
      </div> -->

        <div class="layui-form-item layui-row">
          <div class="layui-col-md12">
            <div id="addQualityItemTableListBox">
              <table class="layui-table" id="addQualityItemTableList" lay-filter="addQualityItemTableList" style="margin: 1px 0 10px; display: none"></table>
            </div>
            <div id="simpleTableBox">
              <div class="layui-col-md6">

                <table class="layui-table" id="simpleTable" lay-filter="simpleTable" style="margin: 1px 0 10px; display: none"></table>
              </div>
              <div class="layui-col-md6" style="padding-left: 30px;">
                <button class="layui-btn" type="button" id="addExceptionBtn">新增异常</button>
                <table class="layui-table" id="exceptionTable" lay-filter="exceptionTable" style="margin: 1px 0 10px"></table>
              </div>
            </div>
          </div>
        </div>

        <!--      <div class="layui-form-item layui-row">
                <div class="layui-col-md12">
                    <table class="layui-table" id="addQualityDetailRecordTableList"
                           lay-filter="addQualityDetailRecordTableList" style="margin: 1px 0 10px;"></table>
                </div>
            </div>-->
      </div>
      <!--      <div class="layui-form-item text-right pd-20" style="padding-top: 10px !important;">
            &lt;!&ndash;        <button class="layui-btn" lay-filter="qualityRecordDetailFormSubmit" lay-submit>保存</button>&ndash;&gt;
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>-->
      <div class="layui-form-item text-right pd-20" style="padding-top: 10px !important">
        <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">
          取消
        </button>
        <button class="layui-btn" id="qualityRecordDetailAndValueFormId" type="button" lay-filter="qualityRecordDetailAndValueFormSubmit" lay-submit type="button">
          保存
        </button>
      </div>
    </div>
</form>

<!--选择质检任务单-->
<script type="text/html" id="selectQualityTaskTemplate">
  <div class="layui-fluid">
    <div class="layui-card">
      <div class="layui-card-body">
        <!-- 头部操作栏 -->
        <div class="layui-form toolbar">
          <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label w-auto">质检任务单编号：</label>
              <div class="layui-input-inline mr0" style="width: 240px;">
                <input
                  name="code"
                  class="layui-input"
                  type="text"
                  placeholder="质检任务编号"
                />
              </div>
            </div>
            <div class="layui-inline">
              <button
                class="layui-btn icon-btn"
                lay-filter="selectTheWorkProcessFormSubSearchLog"
                lay-submit
                style="padding: 0 24px;"
              >
                搜索
              </button>
              <button id="selectTaskQualityClose" class="layui-btn icon-btn">
                确认选择
              </button>
            </div>
          </div>
        </div>
        <!-- 表格 -->
        <table
          class="layui-table"
          id="selectQualityTaskTable"
          lay-filter="selectQualityTaskTable"
        ></table>
      </div>
    </div>
  </div>
</script>
<!--单独修改图片-->
<!--imgModifyForm-->
<script type="text/html" id="imgModifyForm">
  <form
    lay-filter="imgModifyForm"
    class="layui-form model-form"
    style="padding: 5px 30px 0 30px;height: calc(100% - 5px);overflow: hidden;"
  >
    <div class="open-details-box" style="height: calc(100% - 75px);">
      <div class="layui-row table-bor " style="margin-top: 15px;">
        <div class="layui-form-item">
          <div id="productPic_path" class="uploadImgStyle"></div>
          <div id="productPicBtn" class="layui-btn" style="float: left;">
            <i class="layui-icon">&#xe681;</i>上传图片
          </div>
          <input
            name="productPic"
            type="hidden"
            class="layui-input"
            lay-verify=""
            id="productPic"
          />
        </div>
      </div>
    </div>
    <div class="layui-form-item text-right">
      <button
        class="layui-btn layui-btn-primary"
        type="button"
        ew-event="closeDialog"
      >
        关闭
      </button>
    </div>
  </form>
</script>

<script type="text/html" id="viewAttachmentHtml">
  <div id="example1" style="height: 100%;"></div>
</script>
<!-- 表格操作列 -->
<script type="text/html" id="productDetailValueTableBar">
  <!--    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>-->
<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="imgClick">上传图片</a>
</script>

<script type="text/html" id="qualityItemTableBar">
  <!--    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="edit">修改</a>-->
<!-- {{# if(d.id===''){ }} -->
<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="remove">删除</a>
<!-- {{# } }} -->
<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="check">检测</a>
</script>

<!-- js部分 -->
<script>
  layui.use(
    [
      "layer",
      "form",
      "table",
      "laydate",
      "admin",
      "laytpl",
      "upload",
      "config",
    ],
    async function () {
      var $ = layui.jquery;
      var layer = layui.layer;
      var config = layui.config;
      var upload = layui.upload;
      var table = layui.table;
      var admin = layui.admin;
      var form = layui.form;

      let addQualityTestingRecordTableData = [];
      let addQualityDetailRecordTableData = [];

      let _type = "";
      let initData = {};
      let addQualityDetailRecordList = [];
      const nowData = { ...parentData };
      let warehouseList = [];
      if (nowData) {
        if (nowData.data.eventName == "1") {
          $("#addQualityItemTableListBox").show();
          $("#simpleTableBox").hide();
          $("#qualityTestingResultBox").show();
          $("#sampleCodeBox").show();
        } else {
          $("#addQualityItemTableListBox").hide();
          $("#simpleTableBox").show();
          $("#qualityTestingResultBox").hide();
          $("#sampleCodeBox").hide();
          const warehouseRes = await admin.reqPro(
            "enterprisecab/warehouse/admin/warehouse/list",
            { limit: 9999, status: 0 }
          );
          warehouseList = warehouseRes.data;
        }
      }
      //选择开工单
      let templateData = admin.getTempData("taskQualityRecordData");
      console.log('templateData', templateData);
      console.log('nowData', nowData);

      //---------------------------------------------------------------------------

      let addQualityItemTableList = [];

      const exceptionList = await admin.initDataSelectPro(
        "production/quality/admin/taskQualityRecord/getAbnormalCauseBy/" +
        templateData.processId,
        {},
        "exception",
        "异常原因",
        null,
        "name"
      );

      form.on("select(sampleCode)", function (data) {
        admin.reqSync(
          "production/quality/admin/taskQualityRecord/getDetailRecordValue",
          {
            taskId: templateData.id,
            recordDetailId: data.value,
            bookId: templateData.bookId,
          },
          (res) => {
            if (res.code == 200) {
              initData = res.data;
              $("#statusName").val(initData.statusDesc);
              //初始化检测项目
              addQualityItemTableList = res.data.detailValueInfoVos;
              table.reload("addQualityItemTableList", {
                data: addQualityItemTableList,
              });
            }
          },
          "GET"
        );
      });
      //检测类型
      const itemTypeRes = await admin.reqPro(
        "enterprise/dict/admin/dictLabel/select",
        { dictType: "sys_qc_quality_type" }
      );
      const itemTypeList = itemTypeRes.data;
      //质检方法
      const qualityWayRes = await admin.reqPro(
        "enterprise/dict/admin/dictLabel/select",
        { dictType: "sys_detection_method" }
      );
      const qualityWayList = qualityWayRes.data;
      // 质检设备
      const deviceModelSpuRes = await admin.reqPro(
        "enterprise/device/admin/deviceModel/list",
        { limit: 9999 }
      );
      const deviceModelSpuList = deviceModelSpuRes.data;
      // 重要程度
      const importanceRes = await admin.reqPro(
        "enterprise/dict/admin/dictLabel/select",
        { dictType: "pro_importance_type" }
      );
      const importanceList = importanceRes.data;

      let addQualityDetailRecordTable2 = table.render({
        elem: "#addQualityItemTableList",
        data: addQualityItemTableList,
        id: "addQualityItemTableList",
        limit: 9999,
        cols: [
          [
            { type: "numbers", title: "序号", align: "center" },
            {
              field: "processQualityName",
              title: "检测项目",
              align: "center",
            },
            {
              field: "remark",
              // edit: "text",
              title: "质检标准",
              align: "center",
            },
            // {
            //   field: "no",
            //   title: "检测项目编码",
            //   align: "center",
            // },
            // { field: "utensil", title: "检测分类", align: "center" },
            {
              field: "itemType",
              title: "检测类型",
              align: "center",
              templet: (d) => {
                return itemTypeList.find((v) => v.labelValue == d.itemType)
                  ? itemTypeList.find((v) => v.labelValue == d.itemType)
                    .labelName
                  : "-";
              },
            },
            {
              field: "way",
              title: "检验方法",
              align: "center",
              templet: (d) => {
                return qualityWayList.find((v) => v.labelValue == d.way)
                  ? qualityWayList.find((v) => v.labelValue == d.way).labelName
                  : "-";
              },
            },
            {
              field: "utensil",
              title: "检测器具",
              align: "center",
              templet: (d) => {
                return deviceModelSpuList.find((v) => v.id == d.utensil)
                  ? deviceModelSpuList.find((v) => v.id == d.utensil).name
                  : "-";
              },
            },
            {
              field: "importance",
              title: "重要程度",
              align: "center",
              templet: (d) => {
                return importanceList.find((v) => v.labelValue == d.importance)
                  ? importanceList.find((v) => v.labelValue == d.importance)
                    .labelName
                  : "-";
              },
            },
            // { field: "standard", title: "标准/规格", align: "center" },
            {
              field: "processQualityValueName",
              edit: "text",
              title: "检测结果(必填)",
              align: "center",
            },

            //  {
            //  field: "fileName",
            //    edit: "text",
            //    title: "图片名称",
            //    align: "center",
            //  },
            //  {
            //    field: "fileUrl",
            //    edit: "text",
            //    title: "图片地址",
            //    align: "center",
            //  },
            {
              align: "center",
              toolbar: "#productDetailValueTableBar",
              title: "操作",
              width: 300,
            },
          ],
        ],
      });

      table.render({
        elem: "#simpleTable",
        data: addQualityItemTableList,
        id: "simpleTable",
        limit: 9999,
        data: [
          {
            result: "合格",
          },
          {
            result: "返工",
          },
          {
            result: "报废",
          },
        ],
        cols: [
          [
            { type: "numbers", title: "序号", align: "center" },
            {
              field: "result",
              title: "质检结果",
              align: "center",
            },
            {
              field: "simpleNum",
              title: "数量",
              templet: admin.setTableInput("simpleNum",false,'simpleNum'),
              align: "center",
            },
            {
              field: "warehouse",
              title: "入库仓库",
              align: "center",
              templet: admin.setTableSelect(
                "warehouse",
                warehouseList.map((v) => {
                  v.label = v.name + "(" + v.warehouseTypeName + ")";
                  v.value = v.id;
                  return v;
                }), false,
                  "warehouse"

              ),
            },
            // {
            //   field: "cangku",
            //   title: "入库仓库",
            // },
            // {
            //   field: "beizhu",
            //   title: "备注",
            //   templet: admin.setTableInput("beizhu "),
            // },
          ],
        ],
        done: () => {
          $("#simpleTableBox .layui-table-body").css("overflow", "visible");
          $("#simpleTableBox .layui-table-box").css("overflow", "visible");
          $("#simpleTableBox .layui-table-view").css("overflow", "visible");
          admin.tableSelectChange("simpleTable", "warehouse");
          admin.tableInputChangeByClass("simpleTable", "simpleNum");
        },
      });
      console.log('exceptionList', exceptionList);

      table.render({
        elem: "#exceptionTable",
        data: addQualityItemTableList,
        id: "exceptionTable",
        limit: 9999,
        height: 400,
        data: [
        ],
        cols: [
          [
            {
              field: "errorId",
              title: "异常原因",
              align: "center",
              templet: admin.setTableSelect("errorId", exceptionList.map(v => {
                v.value = v.id;
                v.label = v.name;
                return v;
              })),
            },
            // {
            //   field: "errorId",
            //   title: "异常id",
            //   align: "center",
            //   hide: true,
            // },
            {
              field: "errorQuantity",
              title: "数量",
              templet: admin.setTableInput("errorQuantity"),
              align: "center",
            },
            {
              field: "remark",
              title: "备注",
              align: "center",
              templet: admin.setTableInput("remark"),

            },
          ],
        ],
        done: () => {
          // $("#simpleTableBox .layui-table-body").css("overflow", "visible");
          // $("#simpleTableBox .layui-table-box").css("overflow", "visible");
          // $("#simpleTableBox .layui-table-view").css("overflow", "visible");
          admin.tableInputChange("exceptionTable");
        },
      });

      $("#addExceptionBtn").click(function () {
        let list = table.cache["exceptionTable"];
        list.push({
          // errorName:'',
          errorId: '',
          errorQuantity: 1,
          remark: '',
        });
        table.reload("exceptionTable", {
          data: list,
        });
      });
      //工具条的点击事件
      table.on("tool(addQualityItemTableList)", function (obj) {
        if (obj.event === "imgClick") {
          imgModify(obj);
        }
      });

      function imgModify(obj) {
        admin.open({
          type: 1,
          area: ["900px", "500px"],
          offset: "65px",
          title: "上传图片",
          content: $("#imgModifyForm").html(),
          success: function () {
            form.val("imgModifyForm", obj.data);
            if (obj.data.fileUrl) {
              $("#productPic_path").html(
                '<img class="checkImg" style="max-height:350px; max-width: 700px" src="' +
                obj.data.fileUrl +
                '"/>'
              );
            }
            //上传图片
            $("#productPicBtn").click(
              uploadImg(
                "productPicBtn",
                "productPic_path",
                "productPic",
                true,
                obj
              )
            );
          },
        });
      }
      //=====================================================================================

      var qualityDataList = [];
      //修改
      if (templateData) {
        //显示值
        form.val("taskQualityRecordForm", templateData);
        $("#taskQualityMO").val(templateData.code);
        $("#taskId").val(templateData.id);
        $("#testingCategoryName").val(templateData.testingCategoryName);
        $("#testingTypeName").val(templateData.testingTypeName);
        /*  $("#qualityTestingNatureName").val(templateData.qualityTestingNatureName);*/
        $("#planCode").val(templateData.processName);
        $("#carrierCode").val(templateData.carrierCode);
        $("#qcQualityTypeName").val(templateData.qcQualityTypeName);

        //初始化样品配置信息
        //getRecordResultsData
        admin.req(
          "production/quality/admin/taskQualityRecord/getRecordResultsData",
          { recordId: templateData.recordId, processId: null },
          function (res) {
            if (res.code == 200) {
              if (res.data.length > 0) {
                //TODO 样品列表
                qualityDataList = res.data;
                console.log(1111, qualityDataList);
                addQualityDetailRecordTable.reload({ data: qualityDataList });
                let html = '<option value="">请选择样品编码</option>';
                for (var k = 0; k < res.data.length; k++) {
                  if (res.data[k].status == "1") {
                    html +=
                      '<option value="' +
                      res.data[k].id +
                      '">' +
                      res.data[k].sampleCode +
                      "</option>";
                  }
                }
                $("#sampleCode").html(html);
                // 重新渲染表单中的 select 元素
                form.render("select");
                /*  form.render('select');
                          if (data && data.qualityWayId) {
                              form.val('productItemForm', data);
                          }*/
              }
            } else {
              layer.msg(res.msg, { icon: 2 });
            }
          },
          "GET"
        );
      } else {
        $("#taskQualityMO").click(function () {
          showSelectTheWorkProcessTemplateModel();
        });
      }
      function showSelectTheWorkProcessTemplateModel() {
        let selectQualityTaskTemplate = admin.open({
          type: 1,
          title: "选择质检任务",
          area: "82%",
          offset: ["15%", "16%"],
          content: $("#selectQualityTaskTemplate").html(),
          success: function () {
            form.render("select");
            // 渲染表格
            let selectQualityTaskTable = table.render({
              elem: "#selectQualityTaskTable",
              url:
                config.base +
                "production/quality/admin/workOrderQualityTask/list",
              page: true,
              cellMinWidth: 100,
              cols: [
                [
                  { type: "radio" },
                  { field: "code", title: "质检任务单编码" },
                  { field: "processName", title: "所属工序" },
                  { field: "qualityTestingNatureName", title: "质检性质" },
                  { field: "testingCategoryName", title: "质检分类" },
                  { field: "testingTypeName", title: "质检性质" },
                  { field: "workOrderCode", title: "所属开工单" },
                  { field: "deviceCode", title: "送检工站" },
                  { field: "skuCode", title: "物料编码" },
                  { field: "creatorName", title: "送检人" },
                  { field: "submissionQuantity", title: "送检数量" },
                  { field: "statusDesc", title: "状态" },
                ],
              ],
            });

            // 搜索
            form.on(
              "submit(selectTheWorkProcessFormSubSearchLog)",
              function (data) {
                selectQualityTaskTable.reload(
                  {
                    where: data.field,
                    page: { curr: 1 },
                  },
                  "data"
                );
              }
            );
          },
        });
      }

      table.on("tool(addQualityDetailRecordTableList)", function (obj) {
        //删除
        if (obj.event === "remove") {
          obj.del(); // 删除当前行
          obj = null;
          // 获取表格数据
          addQualityDetailRecordTableData =
            table.cache["addQualityDetailRecordTableList"];
          let data = [];
          addQualityDetailRecordTableData.forEach((item) => {
            if (item.sampleCode !== undefined) {
              data.push(item);
            }
          });
          table.reload("addQualityDetailRecordTableList", {
            data: data,
          });
        }
        //检测
        else if (obj.event === "check") {
          let data = {};
          data.taskId = templateData.id;
          data.bookId = templateData.bookId;

          data.recordDetailId = obj.data.id;
          //判定结果
          data.qualityTestingResult = obj.data.qualityTestingResult;
          //检测备注
          data.JCRemark = obj.data.remark;
          //送检工位 2
          data.JCInspectDeviceCode = obj.data.inspectDeviceCode;
          admin.putTempData("taskQualityDetailValueData", data);
          var detectionIndex = admin.formOpen({
            type: 2,
            area: ["80%", "92%"],
            offset: ["7%", "17%"],
            style: "font-size: 20px;",
            title: "录入/修改质检参数",
            path: "components/res/quality/taskQualityRecordDetailValueForm.html",
            end: function () {
              //重新查询检测样品配置项目
              admin.req(
                "production/quality/admin/taskQualityRecord/getRecordResultsData",
                {
                  recordId: templateData.recordId,
                  processId: templateData.processId,
                },
                function (res) {
                  if (res.code == 200) {
                    if (res.data.length > 0) {
                      addQualityDetailRecordTable.reload({ data: res.data });
                    } 1
                  } else {
                    layer.msg(res.msg, { icon: 2 });
                  }
                },
                "GET"
              );
            },
          });
        }
      });
      form.on("select(orderNoInfo)", function (d) {
        $("dealWay").val("");
        if (d.value == 1) {
          $(".dealWayBox").show();
        } else {
          $(".dealWayBox").hide();
        }
      });

      // // 表单提交事件
      // form.on('submit(qualityRecordDetailFormSubmit)', function (d) {
      //     layer.load(2);
      //     d.field.detailList = table.cache['addQualityDetailRecordTableList'];
      //     ;
      //     //修改 or 详情
      //     if (templateData) {
      //         d.field.id = templateData.id;
      //         d.field.workOrderId = templateData.workOrderId;
      //         d.field.qualityTestingTaskId = templateData.qualityTestingTaskId;
      //         d.field.processId = templateData.processId;
      //     } else {
      //         d.field.workOrderId = addQualityTestingRecordTableData[0].workOrderId;
      //         d.field.qualityTestingTaskId = addQualityTestingRecordTableData[0].id;
      //         d.field.processName = addQualityTestingRecordTableData[0].processName;
      //         d.field.processId = addQualityTestingRecordTableData[0].processId;
      //     }
      //     admin.req(templateData ? 'production/quality/admin/taskQualityRecord/updateQualityDetail' : 'production/quality/admin/taskQualityRecord/addQualityDetail', JSON.stringify(d.field), function (res) {
      //         layer.closeAll('loading');
      //         if (res.code == 200) {
      //             layer.msg(res.msg, {icon: 1});
      //             table.reload('qualityQualityRecordTable');
      //             layer.closeAll('page');
      //         } else {
      //             layer.msg(res.msg, {icon: 2});
      //         }
      //     }, templateData ? 'PUT' : 'POST',true,"application/json; charset=utf-8");
      //     return false;
      // });
      //初始化表格
      let addQualityDetailRecordTable = table.render({
        elem: "#addQualityDetailRecordTableList",
        data: addQualityDetailRecordList,
        id: "addQualityDetailRecordTableList",
        limit: 9999,
        cols: [
          [
            { type: "numbers", title: "序号", fixed: "center" },
            {
              field: "sampleCode",
              edit: "text",
              title: "样品编码",
              fixed: "center",
              width: 200,
            },
            { field: "statusName", title: "状态", fixed: "center" },
            {
              field: "qualityTestingResultName",
              title: "检测结果",
              fixed: "center",
            },
            { field: "inspectDeviceCode", title: "检测工位", fixed: "center" },
            { field: "modifierName", title: "检测人", fixed: "center" },
            { field: "updateTime", title: "检测时间", fixed: "center" },
            {
              field: "remark",
              title: "质检备注",
              edit: "text",
              fixed: "center",
            },
            {
              align: "center",
              toolbar: "#qualityItemTableBar",
              title: "操作",
              width: 200,
            },
          ],
        ],
      });

      form.on("submit(qualityRecordDetailAndValueFormSubmit)", function (d) {

        layer.load(2);
        console.log("提交数据", d.field);
        let submitFieldInfo = {};
        submitFieldInfo.detailValueInfoVos =
          table.cache["addQualityItemTableList"];

        if (nowData.data.eventName == 1) {
          submitFieldInfo.recordId = $("#sampleCode").val();
          console.log(123, exceptionList);

          let exceptionObj = exceptionList.find(item => item.id == $("#exception").val());
          console.log('exceptionObj',exceptionObj);
          if(exceptionObj){
            submitFieldInfo.errorInfoVos = [{
              remark: exceptionObj.remark,
              errorName: exceptionObj.name,
              errorQuantity: 1,
              errorId: exceptionObj.id,
            }];
          }
        } else {
          submitFieldInfo.recordId = qualityDataList[0].id;
          submitFieldInfo.errorInfoVos = table.cache["exceptionTable"].map(v => {
            v.errorName = exceptionList.find(item => item.id == v.errorId).name;
            return v;
          })

        }
        submitFieldInfo.paperSlipPic = d.field.paperSlipPic;
        submitFieldInfo.remark = $("#JCRemark").val();
        submitFieldInfo.qualityTestingResult = $("#qualityTestingResult").val();
        submitFieldInfo.qualityTestingResultName = $(
          "#qualityTestingResult"
        ).val();
        submitFieldInfo.inspectDeviceCode = $("#JCInspectDeviceCode").val();
        submitFieldInfo.inspectDeviceId = "";
        submitFieldInfo.dealWay = $("#dealWay").val();
        let simpleTable = table.cache["simpleTable"];

        submitFieldInfo.okQuantity = simpleTable[0].simpleNum
          ? simpleTable[0].simpleNum
          : 0;
        submitFieldInfo.okWarehouseType = simpleTable[0].warehouse;
        submitFieldInfo.returnQuantity = simpleTable[1].simpleNum
          ? simpleTable[1].simpleNum
          : 0;
        submitFieldInfo.returnWarehouseType = simpleTable[1].warehouse;
        submitFieldInfo.ngQuantity = simpleTable[2].simpleNum
          ? simpleTable[2].simpleNum
          : 0;
        submitFieldInfo.ngWarehouseType = simpleTable[2].warehouse;

        console.log("submitFieldInfo", nowData);
        console.log("submitFieldInfo", submitFieldInfo);
        // return
        admin.req(
          "production/quality/admin/taskQualityRecord/submitResultValue",
          JSON.stringify(submitFieldInfo),
          function (res) {
            layer.closeAll("loading");
            if (res.code == 200) {
              layer.msg(res.msg, { icon: 1 });
              layer.closeAll("page");
              table.reload("addQualityDetailRecordTableList");
              table.reload("workOrderQualityTaskTable");
            } else {
              layer.msg(res.msg, { icon: 2 });
            }
          },
          "POST",
          true,
          "application/json; charset=utf-8"
        );
        return false;
      });

      // 上传文件
      /*
       * _elem: 上传按钮的名字,
       * pathId: 显示图片容器的名字,
       * inputId: 提交文件路径元素的名字,
       * isMultiple: 是不是多文件(单文件为空或者false,多文件true)
       *
       * */
      function uploadImg(btn, pathId, inputId, isMultiple, obj) {
        upload.render({
          elem: "#" + btn,
          url: config.upload_url + "uploadFile", //新上传接口
          data: {},
          accept: "file",
          exts: "jpg|png|RAR|zip|pdf|txt|doc|docx|xls|xlsx|ppt|pptx",
          headers: { Authorization: "Bearer " + config.getToken() },
          done: function (res) {
            if (res.code == 200) {
              layer.msg(res.msg || "上传成功", { icon: 1 });
              let imagUrl = res.data.url;
              let imagName = res.data.fileName;
              if (isMultiple == true) {
                obj.update({
                  fileUrl: imagUrl,
                  fileName: imagName,
                });
                $("#" + pathId).html('<img src="' + imagUrl + '"/>');
                $("#" + inputId).val(imagUrl);
              } else {
                obj.update({
                  fileUrl: imagUrl,
                  fileName: imagName,
                });
                $("#" + pathId).html('<img src="' + imagUrl + '"/>');
                $("#" + inputId).val(imagUrl);
              }
            } else if (res.code == 502) {
              return location.replace("admin/login.html");
            } else {
              layer.msg(res.msg || "上传失败", { icon: 2 });
            }
          },
        });
      }
      // upload.render({
      //   elem: "#uploadBtn",
      //   url: config.upload_url + "uploadFile",
      //   multiple: true,
      //   data: {},
      //   accept: "file",
      //   // 图片和pdf的常见格式",
      //   exts: "jpg|pdf|img|png|jpeg",
      //   headers: { Authorization: "Bearer " + config.getToken() },
      //   done: function (res) {
      //     if (res.code == 200) {
      //       console.log(123, res.data);
      //       // let list = table.cache["drawImageTable"];
      //       // list.push({
      //       //   drawUrl: res.data.url,
      //       //   drawName: res.data.fileName,
      //       // });
      //       // table.reload("drawImageTable", {
      //       //   data: list,
      //       // });
      //     } else if (res.code == 502) {
      //       return location.replace("admin/login.html");
      //     } else {
      //       layer.msg(res.msg || "上传失败", { icon: 2 });
      //     }
      //   },
      // });
      // 初始化上传
      upload.render({
        elem: '#uploadBtn',
        url: config.upload_url + "uploadFile",
        accept: 'images',
        exts: 'jpg|png|gif|jpeg',
        headers: { Authorization: "Bearer " + config.getToken() },
        done: function (res) {
          if (res.code === 200) { // code 为 0 表示成功，根据你后台返回结构调整
            document.getElementById('uploadInput').value = res.data.url; // 设值隐藏域
            document.getElementById('uploadPreview').src = res.data.url;
            layer.msg('上传成功');
          } else {
            document.getElementById('uploadText').innerText = '上传失败：' + res.msg;
          }
        },
        error: function () {
          document.getElementById('uploadText').innerText = '上传接口出错';
        }
      });
    }
  );
</script>
<style>
  #simpleTableBox .layui-table-cell {
    overflow: visible !important;
  }
</style>